package algocasts;

public class P46_maxProfitOnePass {
    public int maxProfitOnePassForce(int[] prices) {
        if (prices.length < 2) return 0;
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++)
            for (int j = i + 1; j < prices.length; j++)
                maxProfit = Math.max(maxProfit, prices[j] - prices[i]);
        return maxProfit;
    }

    public int maxProfitOnePass(int[] prices) {
        if (prices.length < 2 ) return 0;
        int maxProfit = 0, buy = prices[0];
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < buy) buy = prices[i];
            else maxProfit = Math.max(maxProfit, prices[i] -buy);
        }
        return maxProfit;
    }
}
